@extends('manage.layout')

@section('content')
    <section class="content-header">
        <h1>
            增加产品
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="#">Product</a></li>
            <li class="active">Product</li>
        </ol>
    </section>

    <section class="content">
        <div class="row">
            <!-- left column -->
            <div class="col-md-12">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"></h3>
                    </div>
                    <!-- /.box-header -->
                    <!-- form start -->
                    <form action="/product/productAdd" method="post" role="form" style="width: 50%;" enctype="multipart/form-data">
                        {{--{{csrf_field()}}--}}
                        <div class="box-body">
                            <div class="form-group">
                                <label for="exampleInputEmail1">一级分类</label>
                                <select id="setTwo" name="parent_id" class="form-control">
                                    @foreach($category_one as $one)
                                        <option value="{{ $one['id'] }}">{{ $one['name'] }}</option>
                                    @endforeach
                                </select>
                            </div>
							<div class="form-group">
                                <label for="exampleInputEmail1">二级分类</label>
                                <select id='category_two' name="category" class="form-control">
                                    @foreach($category_two as $two)
                                        <option value="{{ $two['id'] }}">{{ $two['name'] }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">产品名称</label>
                                <input type="text" name="name" class="form-control" value="{{old('name')}}" placeholder="Product Name" required="required">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">权重</label>
                                <input type="number" name="rank" min="0" value="0" class="form-control" placeholder="Product rank">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">产品估价</label>
                                {{--<select name="price" class="form-control">--}}
                                    {{--<option value="0-5000">0-5000</option>--}}
                                    {{--<option value="0-5000">5000-10000</option>--}}
                                    {{--<option value="0-5000">10000-20000</option>--}}
                                    {{--<option value="0-5000">20000-50000</option>--}}
                                    {{--<option value="0-5000">50000以上</option>--}}
                                {{--</select>--}}
                                <input type="text" name="price" class="form-control" placeholder="Product price">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">制作周期</label>
                                <input type="text" name="period" class="form-control" value="{{old('period')}}" placeholder="Product period" required="required">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">适用企业</label>
                                <input type="text" name="firm" class="form-control" value="{{old('firm')}}" placeholder="Product firm" required="required">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">绩效系数</label>
                                <input type="text" name="performance" class="form-control" value="{{old('performance')}}" placeholder="Product performance" required="required">
                            </div>
                            {{--<div class="form-group">--}}
                                {{--<label for="exampleInputPassword1">产品图片</label>--}}
                                {{--<input id="file" type="file" name="picture" class="form-control">--}}
                            {{--</div>--}}
                            <div class="form-group">
                                <label for="exampleInputPassword1">产品简介</label>
                                <textarea name="brief" class="form-control" placeholder="Product brief" required="required">{{old('brief')}}</textarea>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">服务细则</label>
                                <textarea name="content" style="width:100%;height:300px;">{{old('content')}}</textarea>
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">备注</label>
                                <textarea name="remark" class="form-control" placeholder="Product remark" required="required">{{old('remark')}}</textarea>
                            </div>
                        </div>
                        <!-- /.box-body -->

                        <div class="box-footer">
                            <button type="submit" class="btn btn-primary">提交</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
@stop

@section('script')
    <script>
        $(function() {
			//富文本编辑器
			var editor;
            KindEditor.ready(function(K) {
                editor = K.create('textarea[name="content"]', {
                    afterBlur: function () { this.sync(); },
                    resizeType : 1,
                    allowPreviewEmoticons : false,
                    allowImageUpload : false,
//                    syncType:form,
                    items : [
                        'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                        'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                        'insertunorderedlist', '|', 'emoticons',  'link']
                });


            });

			//提交表单
            $("form").submit(function(event) {
                if(editor.text() == ''){
                    layer.msg('产品描述不能为空');
                    return false;
                }
//                //验证是否选择了上传的文件
//                if($('#file').val() == ''){
//                    // layer.msg('请选择需要上传的图片123！');
//                    layer.msg('请选择需要上传的图片！');
//                    return false;
//                }
                event.preventDefault();
                var url = $(this).attr("action");
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
                $.ajax({
                    url: url,
                    type: "POST",
                    data: $("form").serialize(),
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        if(data.code == 100) {
                            window.location.href = "/product";
                            layer.msg(data.msg)
                        }
                        else {
                            layer.msg(data.msg)
                        }
                    }
                });
            });
			//根据一级分类获取二级分类
			$("#setTwo").change(function(){
				var _id = $("#setTwo").val();
				$.ajax({
                    url: '/product/getTwo',
                    type: "GET",
                    data: {parent_id:_id},
                    dataType: "json",
                    success: function(data) {
                        console.log(data);
                        if(data.code == 100) {
							$('#category_two').empty();
							var html = '';
							for(var i=0;i<data['category_two'].length;i++){
								html+='<option value='+data['category_two'][i]['id']+'>'+data['category_two'][i]['name']+'</option>';
							}
							$('#category_two').html(html);
                        }else{
                            layer.msg(data.msg);
                        }
                    }
                });
			});
        });

    </script>

@stop